﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.Power.Business;
using TjZheng.Cache;
using CJAtech.BBS.Business;
using TjZheng.Framework;

namespace TjZheng.Example.MVC.Controllers
{
    [FooFilter("test")]
    public class CacheController : Controller
    {
        public ActionResult Index()
        {
            //MemCache要求实体必须可序列化
            BizUserInfo objBizUserInfo = new BizUserInfo();

            //var objUserInfo = objBizUserInfo.GetRecordCache(1);
            //objUserInfo = objBizUserInfo.GetRecordCache(1);
            //objBizUserInfo.DeleteIDCache(1);
            //objUserInfo = objBizUserInfo.GetRecordCache("", 1);
            //objUserInfo = objBizUserInfo.GetRecordCache("", 1);
            //objUserInfo = objBizUserInfo.GetRecordCache(1);
            //objUserInfo = objBizUserInfo.GetRecordCache(1);
            //objBizUserInfo.DeleteDependentCache("");
            //objUserInfo = objBizUserInfo.GetRecordCache("", 1);
            //objUserInfo = objBizUserInfo.GetRecordCache("", 1);
            //objUserInfo = objBizUserInfo.GetRecordCache(1);
            //objUserInfo = objBizUserInfo.GetRecordCache(1);

            //BizArticleInfo objBizArticleInfo = new BizArticleInfo();
            //var objArticleInfo = objBizArticleInfo.GetRecordCache(2);

            //var objList = objBizUserInfo.GetListCache("", "Id=@Id", "Id".CreateSqlParameters(1));
            //objList = objBizUserInfo.GetListCache("", "Id=@Id", "Id".CreateSqlParameters(1));
            //string cacheKey = objBizUserInfo.CreateCacheKey("Id=@Id", "GetListCache", "Id".CreateSqlParameters(1));
            //objBizUserInfo.DeleteCache("", cacheKey);
            //objList = objBizUserInfo.GetListCache("", "Id=@Id", "Id".CreateSqlParameters(1));
            //objList = objBizUserInfo.GetListCache("", "Id=@Id", "Id".CreateSqlParameters(1));
            int recordCount = 0;
            var objList = objBizUserInfo.GetPageListCache("", 0, 10, out recordCount);
            objList = objBizUserInfo.GetPageListCache("", 0, 10, out recordCount);

            return Content("");
        }

        /*
         *若缓存后，Filter将不执行，如BarFilter
         *不管指不指定VaryByParam，ver变化将会访问后端；但是如果设置VaryByParam = "none"，ver变化不会访问后端
        */
        //[BarFilter]
        [OutputCache(Duration = 10, VaryByParam = "none")]
        public ActionResult PageCache(string ver)
        {
            return View();
        }

        [OutputCache(Duration = 10)]
        public ActionResult JsonCache()
        {
            InvokeResult objInvokeResult = new InvokeResult();
            objInvokeResult.Data = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
        }

        [OutputCache(Duration = 10, VaryByParam = "ver")]
        public ActionResult PageCacheByParam(int? ver)
        {
            return View("PageCache");
        }

        [ChildActionOnly()]
        [OutputCache(Duration = 10)]
        public ActionResult RendChild()
        {
            return Content("RendChild：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }

        //[OutputCache(Duration = 10)]
        public virtual ActionResult PageCacheBase()
        {
            return Content("PageCacheBase：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }
    }
    [FooFilter("FooChildFilter")]
    public class OverridderController : CacheController
    {
        public override ActionResult PageCacheBase()
        {
            //重写后，作用在基类的filter依然有效
            return Content("PageCacheBase：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }
    }
}
